{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# LOPF with coupling to heating sector\n",
    "\n",
    "In this example three locations are optimised, each with an electric bus and a heating bus and corresponding loads. At each location the electric and heating buses are connected with heat pumps; heat can also be supplied to the heat bus with a boiler. The electric buses are connected with transmission lines and there are electrical generators at two of the nodes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pypsa\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "sns.set(rc={\"figure.figsize\": (9, 5)})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network = pypsa.Network()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add three buses of AC and heat carrier each"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(3):\n",
    "    network.add(\"Bus\", \"electric bus {}\".format(i), v_nom=20.0)\n",
    "    network.add(\"Bus\", \"heat bus {}\".format(i), carrier=\"heat\")\n",
    "network.buses"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network.buses[\"carrier\"].value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add three lines in a ring"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(3):\n",
    "    network.add(\n",
    "        \"Line\",\n",
    "        \"line {}\".format(i),\n",
    "        bus0=\"electric bus {}\".format(i),\n",
    "        bus1=\"electric bus {}\".format((i + 1) % 3),\n",
    "        x=0.1,\n",
    "        s_nom=1000,\n",
    "    )\n",
    "network.lines"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Connect the electric to the heat buses with heat pumps with COP 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(3):\n",
    "    network.add(\n",
    "        \"Link\",\n",
    "        \"heat pump {}\".format(i),\n",
    "        bus0=\"electric bus {}\".format(i),\n",
    "        bus1=\"heat bus {}\".format(i),\n",
    "        p_nom=100,\n",
    "        efficiency=3.0,\n",
    "    )\n",
    "network.links"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add carriers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network.add(\"Carrier\", \"gas\", co2_emissions=0.27)\n",
    "network.add(\"Carrier\", \"biomass\", co2_emissions=0.0)\n",
    "network.carriers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add a gas generator at bus 0, a biomass generator at bus 1 and a boiler at all heat buses"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network.add(\n",
    "    \"Generator\",\n",
    "    \"gas generator\",\n",
    "    bus=\"electric bus 0\",\n",
    "    p_nom=100,\n",
    "    marginal_cost=50,\n",
    "    carrier=\"gas\",\n",
    "    efficiency=0.3,\n",
    ")\n",
    "\n",
    "network.add(\n",
    "    \"Generator\",\n",
    "    \"biomass generator\",\n",
    "    bus=\"electric bus 1\",\n",
    "    p_nom=100,\n",
    "    marginal_cost=100,\n",
    "    efficiency=0.3,\n",
    "    carrier=\"biomass\",\n",
    ")\n",
    "\n",
    "for i in range(3):\n",
    "    network.add(\n",
    "        \"Generator\",\n",
    "        \"boiler {}\".format(i),\n",
    "        bus=\"heat bus {}\".format(i),\n",
    "        p_nom=1000,\n",
    "        efficiency=0.9,\n",
    "        marginal_cost=20.0,\n",
    "        carrier=\"gas\",\n",
    "    )\n",
    "\n",
    "network.generators"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add electric loads and heat loads."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(3):\n",
    "    network.add(\n",
    "        \"Load\",\n",
    "        \"electric load {}\".format(i),\n",
    "        bus=\"electric bus {}\".format(i),\n",
    "        p_set=i * 10,\n",
    "    )\n",
    "\n",
    "for i in range(3):\n",
    "    network.add(\n",
    "        \"Load\",\n",
    "        \"heat load {}\".format(i),\n",
    "        bus=\"heat bus {}\".format(i),\n",
    "        p_set=(3 - i) * 10,\n",
    "    )\n",
    "\n",
    "network.loads"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We define a function for the LOPF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def run_lopf():\n",
    "    network.optimize()\n",
    "    df = pd.concat(\n",
    "        [\n",
    "            network.generators_t.p.loc[\"now\"],\n",
    "            network.links_t.p0.loc[\"now\"],\n",
    "            network.loads_t.p.loc[\"now\"],\n",
    "        ],\n",
    "        keys=[\"Generators\", \"Links\", \"Line\"],\n",
    "        names=[\"Component\", \"index\"],\n",
    "    ).reset_index(name=\"Production\")\n",
    "\n",
    "    sns.barplot(data=df, x=\"index\", y=\"Production\", hue=\"Component\")\n",
    "    plt.title(f\"Objective: {network.objective}\")\n",
    "    plt.xticks(rotation=90)\n",
    "    plt.tight_layout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "run_lopf()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, rerun with marginal costs for the heat pump operation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network.links.marginal_cost = 10\n",
    "run_lopf()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, rerun with no CO2 emissions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "network.add(\"GlobalConstraint\", \"co2_limit\", sense=\"<=\", constant=0.0)\n",
    "\n",
    "run_lopf()"
   ]
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "kernelspec": {
   "display_name": "",
   "language": "python",
   "name": ""
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
